﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using OPCAutomation;
using System;

public class Main : MonoBehaviour
{
    public static Main instance;

    [Tooltip("所有车间")]
    public List<WorkShopMono> workShop_List;

    public Button btn;//登录按钮

    public GameObject a;
    private void Awake()
    {
        instance = this;
    }

    void Start()
    {
        btn.onClick.AddListener(() => 
        {
          
        });

    }
    #region 界面初始动效

    /*
     * 用户点击登录后成功后进行主界面初始动画
     */

    public Transform loginLogo;//登录界面logo
    public Transform loginView;//登录界面

    //登录界面初始动画
    public void LoginStart()
    {
        
    }

    //主界面初始动画
    public void ViewStart() 
    {
        UIDown.instance.OnUI();
        UIRight.instance.OnUI();
        UILeft.instance.OnUI();
        UIUpTime.instance.OnUI();
    }

    #endregion

    public OPCServer Server;
    public OPCGroups Groups;
    public OPCGroup Group;
    public OPCItem Item;
    public OPCItems Items;
    public void Connect()
    {
        Server = new OPCServer();
        //Server.Connect("Matrikon.OPC.Simulation");
        //Server.Connect("Kepware.KEPServerEX.V6", "192.168.0.44");
        Server.Connect("SUPCON.VxHistorian.1", "192.168.4.130");
        if (Server.ServerState == Convert.ToInt32(OPCServerState.OPCRunning))
        {
            Console.WriteLine("SUPCON.VxHistorian.1 " + " 192.168.4.130");
            Console.WriteLine("OPC server is connected!");
            Groups = Server.OPCGroups;
            Groups.DefaultGroupDeadband = 0;
            Groups.DefaultGroupIsActive = true;
            Groups.DefaultGroupUpdateRate = 250;
            Group = Groups.Add("a");
            Group.IsSubscribed = true;
            Items = Group.OPCItems;
            //Item = Items.AddItem("组1.TAG1", 2);
            Console.WriteLine("ZHONGXIN$KYZGZJ1_40001");
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40001", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40003", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40005", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40007", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40009", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40011", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40013", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40015", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40017", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40019", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40021", 1);
            Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40022", 1);
            //Item = Items.AddItem("ZHONGXIN$KYZGZJ1_40013", 1);
            Group.DataChange += Group_DataChange;
        }
        else
        {
            Console.WriteLine("OPC server is not connected");
        }
    }

    private void Group_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
    {
        Console.ForegroundColor = ConsoleColor.Green;
        for (int i = 1; i <= NumItems; i++)
        {
            int tmpClientHandle = Convert.ToInt32(ClientHandles.GetValue(i));
            string tmpValue = ItemValues.GetValue(i).ToString();
            string tmpTime = ((DateTime)(TimeStamps.GetValue(i))).ToString();
            Console.WriteLine("Time:" + tmpTime);
            Console.WriteLine("ClientHandle : " + tmpClientHandle.ToString());
            Console.WriteLine("tag value " + tmpValue);

        }
    }

}
